package com.xhwy.util;

import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.net.Inet4Address;
import java.net.UnknownHostException;

//获取springboot项目的访问地址
public class EnvironmentUtil {

    /**
     * 获取项目的访问地址
     * @param environment org.springframework.core.env.
     * @return
     */
    public static String getAccessPath(Environment environment){
        String networkProtocal = "";
        String ip = "";
        String port = "";
        String webApp = "";
        try {
            //网络协议
            networkProtocal = "http";
            //局域网ip
//            ip = Inet4Address.getLocalHost().getHostAddress();
            ip = "localhost";
            //端口号
            port = environment.getProperty("server.port");
            //项目发布名称
            webApp = environment.getProperty("server.servlet.context-path");
            if(webApp == null) webApp = "";
            String path = networkProtocal + "://" + ip + ":" + port + webApp;
            System.out.println(path);
            return path;
//        } catch (UnknownHostException e) {
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
